package cn.hnu.qq.client.controller;

import java.io.IOException;

import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.client.panel.FriendsListPanel;
import cn.hnu.qq.client.thread.ClientServerListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.LogonMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.entity.message.RegistMessage;
import cn.hnu.qq.global.MessageType;

/**
 * 登陆控制器
 * @author jared
 *
 */
public class LogonController extends CenterController{
	
	private static LogonController instance = null;
	
	private LogonController(){
		
	};
	
	public static synchronized LogonController getInstance(){
		if(instance == null){
			instance = new LogonController();
		}
		return instance;
	}

	public void logon(User user) {
		log.debug("进入 LogonController logon");
		LogonMessage lm = new LogonMessage();
		lm.setMessageType(MessageType.LOGON);
		lm.setPassword(user.getPassword());
		lm.setUserName(user.getAcount());
		lm.setClientListenPort(Integer.valueOf(clientProperties.getProperty("client.port")));
		try {
			getOutput().writeObject(lm);
			getOutput().flush();
			
			System.out.println("LogonController logon写入对象");
//			waitingMessageArriving(MessageType.LOGON_SUCCESS);
//			System.out.println(getCurUser());
//			System.out.println(getCurUser().getFriends());
//			System.out.println(getCurUser().getCrowds());
////			
//			new FriendsListPanel();
			
		} catch (IOException e) {
			System.err.println("登陆服务器失败");
			e.printStackTrace();
		}
	}

	public void regist(User user) {
		log.debug("注册账号："+user);
		RegistMessage rm = new RegistMessage();
		rm.setUser(user);
		try {
			getOutput().writeObject(rm);
			getOutput().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}	
}
